//
//  ResetPassViewController.m
//  Marketing
//
//  Created by wmm on 16/2/28.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "ResetPassViewController.h"
#import "AFNetworking.h"
#import "JudgeNumber.h"
#import "MyTabBarController.h"
#import "DataTool.h"

@interface ResetPassViewController ()

@property (nonatomic, strong) UITextField *passFld;
@property (nonatomic, strong) UITextField *passFld2;

@property (nonatomic, strong) UIButton *submitBtn;

@end

@implementation ResetPassViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGFloat statusBarHeight = 0;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
    {
        statusBarHeight = 20;
    }
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"重置密码"];
    self.navigationItem.leftBarButtonItem=[ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
     self.navigationController.navigationBar.translucent = NO;
    UIImageView *passImg = [[UIImageView alloc] initWithFrame:CGRectMake(20, [UIView getWidth:30.0f], 45, 45)];//@"HY_login_icon_password1",@"HY_login_icon_repeat_password.png"
    passImg.image = [UIImage imageNamed:@"HY_login_icon_password1"];
    [self.view addSubview:passImg];
//    UILabel *passLab = [[UILabel alloc] initWithFrame:CGRectMake(50,passImg.y, 80, 22)];
//    passLab.text = @"新密码";
//    passLab.textColor = blackFontColor;
//    passLab.font = [UIFont systemFontOfSize:16];
//    [self.view addSubview:passLab];
    _passFld = [[UITextField alloc] initWithFrame:CGRectMake(passImg.maxX + 10.0f,passImg.y + 8, KSCreenW-150, 22)];
    _passFld.placeholder = @"请输入新密码";
    _passFld.secureTextEntry = YES;//密码
    [_passFld setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
    _passFld.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:_passFld];
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(_passFld.x,passImg.maxY, KSCreenW-_passFld.x, 1)];
    line.layer.borderWidth = 18;
    line.layer.borderColor = [grayLineColor CGColor];
    [self.view addSubview:line];
    
    UIImageView *passImg2 = [[UIImageView alloc] initWithFrame:CGRectMake(20,passImg.y+57, 45, 45)];
    passImg2.image = [UIImage imageNamed:@"HY_login_icon_repeat_password.png"];
    [self.view addSubview:passImg2];
//    UILabel *passLab2 = [[UILabel alloc] initWithFrame:CGRectMake(50,passImg.y+57, 80, 22)];
//    passLab2.text = @"确认密码";
//    passLab2.textColor = blackFontColor;
//    passLab2.font = [UIFont systemFontOfSize:16];
//    [self.view addSubview:passLab2];
    _passFld2 = [[UITextField alloc] initWithFrame:CGRectMake(_passFld.x,passImg2.y + 8, KSCreenW-150, 22)];
    _passFld2.placeholder = @"请再输入一遍密码";
    _passFld2.secureTextEntry = YES;//密码
    _passFld2.returnKeyType =UIReturnKeyGo;//return键
    [_passFld2 setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    _passFld2.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:_passFld2];
    UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(line.x,passImg2.maxY - 5, line.width, 1)];
    line2.layer.borderWidth = 18;
    line2.layer.borderColor = [grayLineColor CGColor];
    [self.view addSubview:line2];
    
    
    UIView *view = [ViewTool getLineViewWith:CGRectMake(0, KSCreenH  - 70.0f - 64, KSCreenW, 70.0f) withBackgroudColor:[UIColor whiteColor]];
    [self.view addSubview:view];
    
    _submitBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [_submitBtn setTitle:NSLocalizedString(@"确认提交", nil) forState:UIControlStateNormal];
    [_submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_submitBtn.titleLabel setFont:[UIFont systemFontOfSize:20]];
//    [_submitBtn setBackgroundColor:redBackColor];
    [_submitBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_normal.png"] forState:UIControlStateNormal];
    [_submitBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_press.png"] forState:UIControlStateHighlighted];
    _submitBtn.layer.cornerRadius = 4.0;//圆角
    _submitBtn.frame = CGRectMake([UIView getWidth:10.0f] , 5, KSCreenW- 2 * [UIView getWidth:10.0f], view.height - 10);
    _submitBtn.userInteractionEnabled=NO;//不可点击
//    _submitBtn.alpha=0.4;//变灰
    [_submitBtn addTarget:self action:@selector(sumitPass) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:_submitBtn];
    
    _passFld.delegate = self;
    _passFld2.delegate = self;
    
    [_passFld addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    [_passFld2 addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sumitPass{
    NSLog(@"sumitPass");
    
    if (_passFld.text == nil | [_passFld.text length] == 0 ){
        [self.view makeToast:@"请输入密码"];
        return;
    }else if (_passFld2.text == nil | [_passFld2.text length] == 0 ){
        [self.view makeToast:@"请再次输入密码"];
        return;
    }else if ([_passFld.text length] < 6 | [_passFld2.text length] < 6){
        [self.view makeToast:@"密码不小于6位"];
        return;
    }
    if (![_passFld.text isEqualToString:_passFld2.text]) {
        [self.view makeToast:@"两次密码不一致，请重新输入"];
        return;
    }

//    [self.view makeToastActivity];
    [SVProgressHUD show];
    NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
    
    NSDictionary * params = [NSDictionary dictionaryWithObjectsAndKeys:username, @"username",_passFld.text, @"password",_fwdKey,@"fwdKey", nil];
    NSLog(@"%@",params);
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer.timeoutInterval = 20;
    [manager POST:MODIFY_PASS_URL parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        [self.view hideToastActivity];
        [SVProgressHUD dismiss];
        NSLog(@"%@",responseObject);
        int code = [[(NSDictionary *)responseObject objectForKey:@"code"] intValue];
        NSString * message = [(NSDictionary *)responseObject objectForKey:@"message"];
//        [self.view makeToast:message];
        NSLog(@"%@",message);
        if(code != 100)
        {
            NSString * message = [(NSDictionary *)responseObject objectForKey:@"message"];
            [self.view makeToast:message];
        }else{
//            [self.view makeToast:@"修改成功"];
//            [self modifyToLogin:username withPass:_passFld.text];
            [DataTool login:username withPass:_passFld.text fromView:self];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
        [self.view hideToastActivity];
        [SVProgressHUD dismiss];
        [self.view makeToast:@"修改失败"];
    }];
}

- (void)textFieldChanged:(UITextField *)textField{
    if (textField == _passFld) {
        if ([_passFld.text length] > 20) {
            _passFld.text = [_passFld.text substringToIndex:20];
        }
    }
    if (textField == _passFld2) {
        if ([_passFld2.text length] > 20) {
            _passFld2.text = [_passFld2.text substringToIndex:20];
        }
    }
    if(_passFld.text != nil & [_passFld.text length] != 0 & _passFld2.text != nil & [_passFld2.text length] != 0 ){
        _submitBtn.userInteractionEnabled=YES;//可点击
        _submitBtn.alpha=1;
    }else{
        _submitBtn.userInteractionEnabled=NO;
        _submitBtn.alpha=0.4;
    }
}

//关闭虚拟键盘。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if(textField == _passFld2){
        [_passFld resignFirstResponder];
        [_passFld2 resignFirstResponder];
        [self sumitPass];
    }
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_passFld resignFirstResponder];
    [_passFld2 resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
